﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JEdwonTune.Lib.Validation;
using JEdwonTune.Common;

namespace JEdwonTune.Validation {
    public partial class FrmEditField : FrmXmlNodeEditorBase {

        public FrmEditField() {
            InitializeComponent();
        }

        private void FrmEditField_Load(object sender, EventArgs e) {
            this.cbxExpress.Enabled = false;
            this.txtCompare.Enabled = false;
            this.txtPattern.Enabled = false;
            if (base.Node == null) {
                return;
            }
            if (base.Node.Name != "field") {
                MessageBox.Show("编辑的节点不是field节点");
                this.Close();
                return;
            }
            String _prefix = base.TheParent.Attributes["client-prefix"].Value;
            if (!String.IsNullOrEmpty(_prefix)) {
                this.txtPrefix.Text = this.txtPrefix2.Text = _prefix;
            }

            if (base.Node.Attributes["id"] != null) {
                this.txtId.Text = base.Node.Attributes["id"].Value;
            }
            if (base.Node.Attributes["name"] != null) {
                this.textName.Text = base.Node.Attributes["name"].Value;
            }
            this.cbxType.DataSource = Ctrls.VdTypeItems();
            this.cbxType.DisplayMember = "Value";
            this.cbxType.ValueMember = "Key";
            if (base.Node.Attributes["type"] != null) {
                this.cbxType.SelectedValue = base.Node.Attributes["type"].Value;
            }

            if (base.Node.Attributes["pattern"] != null) {
                this.txtPattern.Text = base.Node.Attributes["pattern"].Value;
            }

            if (base.Node.Attributes["compare"] != null) {
                this.txtCompare.Text = base.Node.Attributes["compare"].Value;
            }

            this.cbxExpress.DataSource = Ctrls.VdExpressItems();
            this.cbxExpress.DisplayMember = "Value";
            this.cbxExpress.ValueMember = "Key";
            if (base.Node.Attributes["express"] != null) {
                this.cbxExpress.SelectedValue = base.Node.Attributes["express"].Value;
            }

            if (base.Node.Attributes["server"] != null) {
                String s = base.Node.Attributes["server"].Value;
                this.cb_RunAtServer.Checked = (s.ToLower() == "true"|| s.ToLower()=="1" || s.ToLower()=="yes");
            }

            if (base.Node.InnerText != null) {
                this.txtMessage.Text = base.Node.InnerText;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Abort;
            if (String.IsNullOrEmpty(this.txtId.Text.Trim())) {
                MessageBox.Show("请输入id");
                return;
            }
            SetNodeAttrValue(base.Node,"id", this.txtId.Text.Trim());
            SetNodeAttrValue(base.Node, "name", this.textName.Text.Trim());
            SetNodeAttrValue(base.Node, "type", this.cbxType.SelectedValue.ToString());
            SetNodeAttrValue(base.Node,"pattern", this.txtPattern.Text.Trim());
            SetNodeAttrValue(base.Node,"compare", this.txtCompare.Text.Trim());
            SetNodeAttrValue(base.Node,"express", this.cbxExpress.SelectedValue.ToString());

            if (cb_RunAtServer.Checked) {
                SetNodeAttrValue(base.Node, "server", "true");
            } else {
                RemoveNodeAttr(base.Node, "server");
            }

            base.Node.InnerXml = "<![CDATA["+this.txtMessage.Text.Trim()+"]]>";
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void cbxType_SelectedIndexChanged(object sender, EventArgs e) {
            ComboBox cbx = (ComboBox)sender;

            if (cbx.SelectedValue.ToString() == "regex") {
                this.txtPattern.Enabled = true;
                this.txtCompare.Enabled = false;
                this.cbxExpress.Enabled = false;
            } else if (cbx.SelectedValue.ToString() == "compare" || cbx.SelectedValue.ToString() == "equal") {
                this.txtPattern.Enabled = false;
                this.txtCompare.Enabled = true;
                this.cbxExpress.Enabled = true;
            } else {
                this.txtPattern.Enabled = false;
                this.txtCompare.Enabled = false;
                this.cbxExpress.Enabled = false;
            }
        }
    }
}
